package com.jsb.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.annotation.SystemControllerLog;
import com.jsb.model.Rotation;
import com.jsb.model.RotationExample;
import com.jsb.service.RotationService;

@Controller
public class RotationController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/rotation/list.do";
	
	@Resource
	private RotationService rotationService;
	
	@RequestMapping("/rotation/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		RotationExample rotationExample = new RotationExample();
		int totalCount = rotationService.countRotation(rotationExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		rotationExample.setPageIndex((pageNum - 1) * pageSize);
		rotationExample.setPageSize(pageSize);
		rotationExample.setOrderByClause("orderNum asc");
		List<Rotation> rotations = rotationService.getPageRotation(rotationExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("rotations", rotations);
		return "/Rotation/list";
	}
	
	@RequestMapping("/rotation/toAdd")
	public String toAdd(Model model) {
		return "/Rotation/add";
	}

	@RequestMapping("/rotation/add")
	@SystemControllerLog(description = "添加轮播图")
	public String add(Model model, @ModelAttribute("rotation") Rotation rotation) {
		String id = UUID.randomUUID().toString().replaceAll("-", "");
		rotation.setUuid(id);
				
		rotationService.addRotation(rotation);
		return redirect;
	}
//	@RequestMapping("/rotation/add")
//	public String add(Model model, @ModelAttribute("rotation") Rotation rotation,MultipartFile pic,HttpServletRequest request) throws Exception, IOException {
//		String id = UUID.randomUUID().toString().replaceAll("-", "");
//		rotation.setUuid(id);
//		
//		String filename= pic.getOriginalFilename();
//		String suffix= FilenameUtils.getExtension(filename);
//		String uuid = UUID.randomUUID().toString().replace("-", "");
//		String newName=uuid+"."+suffix;
//		String realPath = request.getSession().getServletContext().getRealPath("")+"getPic\\rotation\\";
//		String imgUrl = newName;
//		String allUrl = realPath+imgUrl;
//		//上传
//		pic.transferTo(new File(allUrl));
//		rotation.setImage(uuid);
//		
//		rotationService.addRotation(rotation);
//		return redirect;
//	}

	@RequestMapping("/rotation/del")
	@SystemControllerLog(description = "删除轮播图")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		rotationService.delRotationByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/rotation/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Rotation rotation = rotationService.getRotationByUuid(uuid);
		model.addAttribute("rotation", rotation);
		return "/Rotation/edit";
	}

	@RequestMapping("/rotation/edit")
	@SystemControllerLog(description = "编辑轮播图")
	public String edit(Model model, @ModelAttribute("rotation") Rotation rotation) {
		rotationService.updateRotation(rotation);
		return redirect;
	}

	@RequestMapping("/rotation/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Rotation rotation = rotationService.getRotationByUuid(uuid);
		model.addAttribute("rotation", rotation);
		return "/Rotation/view";
	}
	@RequestMapping("/rotation/uploadPic.do")
	public void uploadPic(MultipartFile pic,HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException, JSONException{
		String filename= pic.getOriginalFilename();
		String suffix= FilenameUtils.getExtension(filename);
		String name = filename;
		String allUrl = "D:/data/web/rotation/"+name;
		//上传
		pic.transferTo(new File(allUrl));
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", name); // 回显图片
		jsonObject.put("imgUrl", allUrl); // 保存图片地址
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().write(jsonObject.toString());
	}
}
